from django.test import TestCase
import pickle
import base64
# Create your tests here.


if __name__ == "__main__":
    cart_data = 'gAN9cQAoSwF9cQEoWAUAAABjb3VudHECSwJYCAAAAHNlbGVjdGVkcQOJdUsDfXEEKGgCSwFoA4h1dS4='

    # cart_data = cart_data.encode()
    # print(cart_data)
    #
    # cart_data = base64.b64decode(cart_data)
    # print(cart_data)
    #
    # # pickle.loads(bytes字节流)：将bytes字节流转换为对象。
    # cart_dict = pickle.loads(cart_data)

    # 解析cookie中的购物车数据:
    cart_dict = pickle.loads(base64.b64decode(cart_data))
    print(cart_dict)


# if __name__ == "__main__":
#     # pickle.dumps(对象)：将对象转换为bytes字节流。
#     cart_dict = {
#         1: {
#             'count': 2,
#             'selected': False
#         },
#         3: {
#             'count': 1,
#             'selected': True
#         }
#     }
#
#     # cart_data = pickle.dumps(cart_dict)
#     # print(cart_data)
#     #
#     # # base64.b64encode(bytes字节流)：将传入的bytes字节流进行base64编码，返回编码之后的bytes内容
#     # cart_data = base64.b64encode(cart_data)
#     # print(cart_data)
#     #
#     # cart_data = cart_data.decode()
#     # print(cart_data)
#
#     # 设置cookie购物车数据:
#     cart_data = base64.b64encode(pickle.dumps(cart_dict)).decode()
#     print(cart_data)